package com.superyee.commonlib.widgets;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.superyee.commonlib.R;


/**
 * Created by lenovo on 2018/3/3.
 */

public class BottomTabView extends RelativeLayout {


    private ImageView tabIcon;
    private TextView tabTitle, tabCount;
    private ObjectAnimator mObjectAnimator;
    private boolean isSelected = false;

    public BottomTabView(Context context) {
        super(context);
        initView(context, null);
    }

    public BottomTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public BottomTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BottomTabView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.comm_bottom_tab_layout, this);
        tabTitle = findViewById(R.id.tabTitle);
        tabCount = findViewById(R.id.tabCount);
        tabIcon = findViewById(R.id.tabIcon);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.BottomBar_TabView);
            CharSequence title = typedArray.getString(R.styleable.BottomBar_TabView_tabTitle);
            if (title != null) {
                tabTitle.setText(title);
            }
            int icon = typedArray.getResourceId(R.styleable.BottomBar_TabView_tabIcon, 0);
            if (icon != 0) {
                tabIcon.setImageResource(icon);
            }
            int textSize = typedArray.getDimensionPixelOffset(R.styleable.BottomBar_TabView_tabTitleSize, 0);
            if (textSize != 0) {
                tabTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            }
            boolean iconVisible = typedArray.getBoolean(R.styleable.BottomBar_TabView_tabIconVisible, true);
            tabIcon.setVisibility(iconVisible ? View.VISIBLE : View.GONE);
            int titleColor = typedArray.getResourceId(R.styleable.BottomBar_TabView_tabTitleColor, 0);
            if (titleColor != 0) {
                tabTitle.setTextColor(ContextCompat.getColorStateList(context, titleColor));
            }
            typedArray.recycle();
        }
        setBackgroundResource(R.drawable.comm_foreground);
    }

    public void setTitle(String title) {
        tabTitle.setText(title);
    }

    public void setTitleColor(int resource) {
        tabTitle.setTextColor(ContextCompat.getColor(getContext(), resource));
    }

    public void setTitleColorStateList(int resource) {
        tabTitle.setTextColor(ContextCompat.getColorStateList(getContext(), resource));
    }

    public void setIcon(int resource) {
        tabIcon.setImageResource(resource);
    }

    public void setCount(int count) {
        tabCount.setText(String.valueOf(count));
        tabCount.setVisibility(count > 0 ? View.VISIBLE : View.GONE);
    }

    public void setVisible(boolean visible) {
        tabCount.setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    public void setNewIconSize(boolean small) {
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                int size;
                if (small) {
                    size = getResources().getDimensionPixelOffset(R.dimen.dp_12);
                } else {
                    size = getResources().getDimensionPixelSize(R.dimen.dp_20);
                }
                ViewGroup.LayoutParams params = tabCount.getLayoutParams();
                params.width = size;
                params.height = size;
                tabCount.setLayoutParams(params);
            }
        });
    }

    public String getCount() {
        return tabCount.getText().toString();
    }

    public void setSelected(boolean selected) {
        tabTitle.setSelected(selected);
        tabIcon.setSelected(selected);
        isSelected = selected;
        super.setSelected(selected);
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void startRotation() {
        if (mObjectAnimator == null || mObjectAnimator.getTarget() == null) {
            mObjectAnimator = ObjectAnimator.ofFloat(tabIcon, "rotation", 0f, 360f);
            mObjectAnimator.setDuration(5000);
            mObjectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
            mObjectAnimator.setRepeatMode(ObjectAnimator.RESTART);
            mObjectAnimator.setInterpolator(new LinearInterpolator());
        }
        if (!mObjectAnimator.isRunning()) {
            mObjectAnimator.start();
        }
    }

    public void stopRotation() {
        if (mObjectAnimator != null) {
            mObjectAnimator.cancel();
        }
    }

    public ImageView getTabIcon() {
        return tabIcon;
    }

}
